Skip to main content

Traits

  • Define a set of behavior for other types to follow if they want to have this Trait
pub trait Summary {
fn summarize(&self) -> String;
}

pub struct NewsArticle {
pub headline: String,
pub location: String,
pub author: String,
pub content: String,
}

impl Summary for NewsArticle {
fn summarize(&self) -> String {
format!("{}, by {} ({})", self.headline, self.author, self.location)
}
}

pub struct Tweet {
pub username: String,
pub content: String,
pub reply: bool,
pub retweet: bool,
}

impl Summary for Tweet {
fn summarize(&self) -> String {
format!("{}: {}", self.username, self.content)
}
}

use aggregator::{Summary, Tweet}; // Note that when we want to call the function that implements a trait, we also need to import the trait into the scope

fn main() {
let tweet = Tweet {
username: String::from("horse_ebooks"),
content: String::from(
"of course, as you probably already know, people",
),
reply: false,
retweet: false,
};

println!("1 new tweet: {}", tweet.summarize());
}
  • We can also do default implementation when defining a trait
pub trait Summary {
fn summarize(&self) -> String {
String::from("(Read more...)")
}
}
  • Trait as parameters, basically means that any params need to implement the given
pub fn notify(item: &impl Summary) {
println!("Breaking news! {}", item.summarize());
}